<template>
  <div class="plan-filing bg-background text-foreground p-4">
    <h2 class="text-2xl font-bold mb-4">气体浓度信息管理</h2>


    <!-- Search Bar -->
    <div class="bg-card text-card-foreground p-4 mb-4 rounded shadow">

      <QueryForm :model="search" :formItems="formItems" @search="searchPlans" @reset="resetSearch">
        <template #extra>
          <span style="font-size: var(--el-form-label-font-size);color: var(--el-text-color-regular);">检测日期：</span>
          <input type="date" class="input-time-query" style="margin-right:32px" v-model="search.startDate">
          <input type="date" class="input-time-query" v-model="search.endDate">
        </template>
      </QueryForm>
    </div>

    <!-- Action Buttons -->
    <div class="mb-4 space-x-2">
      <button @click="exportData(planData, '气体浓度信息管理')"
        class="bg-secondary text-secondary-foreground px-4 py-2 rounded">导出</button>
    </div>

    <!-- Data Table -->
    <div class="bg-card text-card-foreground my-6 rounded shadow-md">
      <el-table v-loading="loading" :data="displayedPlanData" style="width: 100%" ref="table" align="center"
        class="data-table" :header-cell-style="headerCellStyle">
        <el-table-column prop="MONITORING_TIME" label="监测时间" header-align="center" align="center">
          <template #default="scope">
            {{ dayjs(scope.row.MONITORING_TIME).format('YYYY-MM-DD') }}
          </template>
        </el-table-column>
        <el-table-column prop="OXYGEN" label="氧气（O₂）" header-align="center" align="center"></el-table-column>
        <el-table-column prop="CARBON" label="一氧化碳（CO）" header-align="center" align="center"></el-table-column>
        <el-table-column prop="DIOXIDE" label="二氧化碳（CO₂）" header-align="center" align="center"></el-table-column>
        <el-table-column prop="METHANE" label="甲烷（CH₄）" header-align="center" align="center"></el-table-column>
        <el-table-column prop="HYDROGEN" label="氢气（H₂）" header-align="center" align="center"></el-table-column>
        <el-table-column prop="AMMONIA" label="氨气（NH₃）" header-align="center" align="center"></el-table-column>
        <el-table-column prop="SULFIDE" label="硫化氢（H₂S）" header-align="center" align="center"></el-table-column>

      </el-table>
    </div>
    <!-- Pagination -->

    <Pagination v-if="planData.length" :tableData="planData" @alltabledata="alltabledata" />
    <!--  -->
    <el-dialog class="dialog-from" v-model="isViewDialogOpen" title="记录详情" destroy-on-close align-center
      :before-close="closeViewDialog">
      <div class="body-item">
        <div class="tablename">
          <div
            style="width: 10px; height: 10px; background-color:rgba(2, 125, 180, 1);margin-right: 10px;border:1px solid;">
          </div>
          <span>基本信息</span>
        </div>
        <div class="box-content">
          <div class="grid grid-cols-3 gap-4">
            <div>
              <strong class="no-wrap">单位名称:</strong>
              <span class="auto-wrap">{{ selectedItem.PREPARATION_DEPARTMENT }}</span>
            </div>
            <div>
              <strong class="no-wrap">机构代码:</strong>
              <span class="auto-wrap">{{ selectedItem.DEPARTMENT_CODE }}</span>
            </div>
            <div>
              <strong class="no-wrap">法定代表人:</strong>
              <span class="auto-wrap">{{ selectedItem.DEPARTMENT_REP }}</span>
            </div>
            <!--  -->
            <div>
              <strong class="no-wrap">联系电话:</strong>
              <span class="auto-wrap">{{ selectedItem.DEPARTMENT_REP_PHONE }}</span>
            </div>
            <div>
              <strong class="no-wrap">联系人:</strong>
              <span class="auto-wrap">{{ selectedItem.DEPARTMENT_PER }}</span>
            </div>
            <div>
              <strong class="no-wrap">联系电话:</strong>
              <span class="auto-wrap">{{ selectedItem.DEPARTMENT_PER_PHONE }}</span>
            </div>
            <!--  -->
            <div>
              <strong class="no-wrap">传真：</strong>
              <span class="auto-wrap">{{ selectedItem.DEPARTMENT_FAX }}</span>
            </div>
            <div>
              <strong class="no-wrap">电子邮箱：</strong>
              <span class="auto-wrap">{{ selectedItem.DEPARTMENT_E_MAIL }}</span>
            </div>
            <div>
              <strong class="no-wrap">地址：</strong>
              <span class="auto-wrap">{{ selectedItem.DEPARTMENT_ADDR }}</span>
            </div>
            <!--  -->
            <div>
              <strong class="no-wrap">预案名称：</strong>
              <span class="auto-wrap">{{ selectedItem.EMERGENCE_PLAN_NAME }}</span>
            </div>
            <div>
              <strong class="no-wrap">风险级别：</strong>
              <span class="auto-wrap">{{ selectedItem.PLAN_FILE1 }}</span>
            </div>
            <div>
              <strong class="no-wrap">报送单位：</strong>
              <span class="auto-wrap">{{ selectedItem.SUBMIT_DEPARTMENT }}</span>
            </div>
            <!--  -->
            <div>
              <strong class="no-wrap">备案部门：</strong>
              <span class="auto-wrap">{{ selectedItem.RECORD_DEPART }}</span>
            </div>
            <div>
              <strong class="no-wrap">备案部门性质：</strong>
              <span class="auto-wrap">{{ selectedItem.RECORD_DEPART_NATURE }}</span>
            </div>
            <div>
              <strong class="no-wrap">备案编号：</strong>
              <span class="auto-wrap">{{ selectedItem.RECORD_CODE }}</span>
            </div>
            <!--  -->
            <div>
              <strong class="no-wrap">预案文件：</strong>
              <span class="auto-wrap">{{ selectedItem.EMERGENCE_PLAN_ID }}</span>
              <el-button v-if="selectedItem.EMERGENCE_PLAN_ID" style="width: 75px; height: 30px;">
                <a :href="apiBaseUrl + '/download/' + selectedItem.EMERGENCE_PLAN_ID"
                  :download="selectedItem.EMERGENCE_PLAN_ID">下载查看</a>
              </el-button>
            </div>
            <div>
              <strong class="no-wrap">预案备案登记资料：</strong>
              <span class="auto-wrap">{{ selectedItem.PLAN_TO_RECORD_MATERIAL }}</span>
              <el-button v-if="selectedItem.PLAN_TO_RECORD_MATERIAL" style="width: 75px; height: 30px;">
                <a :href="apiBaseUrl + '/download/' + selectedItem.PLAN_TO_RECORD_MATERIAL"
                  :download="selectedItem.PLAN_TO_RECORD_MATERIAL">下载查看</a>
              </el-button>
            </div>
          </div>
          <!--  -->
          <div style="margin:10px 0">
            <strong class="no-wrap">突发环境事件应急预案备案目录：</strong>
            <span class="auto-wrap">{{ selectedItem.RECORD_CONTENTS }}</span>
          </div>
          <!--  -->
          <div style="margin:10px 0">
            <strong class="no-wrap">备案意见：</strong>
            <span class="auto-wrap">{{ selectedItem.RECORD_COMMENTS }}</span>
          </div>

        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import axios from 'axios'
import { getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance()
import { dayjs } from "element-plus"
import { exportData } from '#/utils/exportfile.js'
const apiBaseUrl = ref(proxy.$currentUrl + '/api/gas_concentration')
// 搜索相关
const search = ref({
  EQUIP_CODE: "",
  startDate: '',
  endDate: '',

})
const formItems = ref([


  {
    label: '设备编号:',
    model: 'EQUIP_CODE',
    type: 'input',
    placeholder: '请输入',
  },
])
const rowKey = (row) => row.ID
const loading = ref(false)
const handleSelectionChange = (selected) => {
  selectedIds.value = selected
}
const headerCellStyle = {
  height: '30px', // 设置表头高度
  lineHeight: '30px', // 设置行高以垂直居中
  backgroundColor: '#f2f2f2', // 可选：设置背景颜色
  // fontSize: '16px', // 可选：设置字体大小
}
// 数据相关
const planData = ref([])
const isViewDialogOpen = ref(false)
const selectedItem = ref(null)
const selectedIds = ref([])
const displayedPlanData = ref([])
const alltabledata = (data) => {
  displayedPlanData.value = data

}
// API 调用函数
const searchPlans = async () => {

  try {
    const response = await axios.get(`${apiBaseUrl.value}/search`, { params: search.value })
    planData.value = response.data.map(app => ({ ...app, selected: false }))
    selectedIds.value = []
  } catch (error) {
    console.error('搜索记录失败:', error)
    alert('搜索记录失败')
  }
}
// 事件处理函数
const resetSearch = () => {
  search.value = {
    EQUIP_CODE: "",
    startDate: '',
    endDate: '',
  }
  searchPlans()
}


const viewDetails = (item) => {
  selectedItem.value = { ...item }
  isViewDialogOpen.value = true
}

const closeViewDialog = () => {
  isViewDialogOpen.value = false
  selectedItem.value = {}
}

// 生命周期钩子
onMounted(() => {
  searchPlans()
})

</script>

<style scoped>
/* 移除所有现有的样式 */
</style>
